package com.hserver.statistics.action.api;

import com.google.gson.Gson;
import com.hserver.statistics.bean.DataStatistics;
import com.hserver.statistics.bean.ShowData;
import com.hserver.statistics.util.JsonResult;
import com.hserver.statistics.util.StatisticsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.hserver.core.interfaces.HttpRequest;
import top.hserver.core.interfaces.HttpResponse;
import top.hserver.core.ioc.annotation.Controller;
import top.hserver.core.ioc.annotation.GET;
import com.hserver.statistics.util.FileUtil;

import java.io.*;
import java.util.*;

@Controller
public class ApiAction {
    private  static Logger log = LoggerFactory.getLogger(ApiAction.class);

    @GET("/statistics")
    public void statistics(HttpResponse response, HttpRequest request) {
        DataStatistics dataStatistics = new DataStatistics();
        String module = request.query("module");
        String anInterface = request.query("interface");
        String name = request.query("name");
        String type = request.query("type");

//        String module = "xx";
//        String anInterface ="aa";
//        String name = "bb";
//        String type = "0";

        if (module != null && anInterface != null && name != null && module.length() > 0 && anInterface.length() > 0 && name.length() > 0) {
            dataStatistics.setModuleName(module);
            dataStatistics.setInterfaceName(anInterface);
            dataStatistics.setSaveName(name);
            dataStatistics.setTimeStr(System.currentTimeMillis());
            if (type==null||type.equals("1"))
            {
                StatisticsUtil.add(dataStatistics, true);
            }
            else {
                StatisticsUtil.add(dataStatistics, false);
            }
            response.sendJson(JsonResult.ok());
        } else {
            response.sendJson(JsonResult.error());
        }
    }

    @GET("/getData")
    public void getData(HttpResponse response, HttpRequest request) {
        String callback = request.query("callback");
        String path = request.query("path");
        String fileName = request.query("fileName");
        List<ShowData> showData = FileUtil.readFile(path + File.separator + fileName);
        String json = new Gson().toJson(showData);
        String s = callback + "(" + json + ")";
        response.sendHtml(s);
    }

    @GET("/getOneFileString")
    public void getOneFileString(HttpResponse response) {
        String s = FileUtil.getRunPath() + File.separator + "db";
        response.sendJson(getFileString(s));
    }

    @GET("/getTwoFileString")
    public void getTwoFileString(HttpResponse response, HttpRequest request) {
        String s = FileUtil.getRunPath() + File.separator + "db" + File.separator + request.query("path");
        response.sendJson(getFileString(s));
    }

    private JsonResult getFileString(String s) {
        try {
            File file = new File(s);
            String[] list = file.list();
            return JsonResult.ok().put("data",list);
        } catch (Exception e) {
            return JsonResult.error();
        }
    }
}
